package com.example.leoss;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.leoss.Duquzifu.Data;
import com.example.leoss.zhuye.img_tongxu_phone.Tongxu_img_Activity;
import com.scwang.smartrefresh.layout.util.SmartUtil;

import java.util.List;

public class Cursor1Adapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context mContext ,vcc;
    private List<Data> list;
    Cursor cursor;
    private View view;
    private LinearLayout laiyout;
    private TextView tvDelete;
    private boolean is=true;
    private ImageView imageView;
    private int i;
    private float downX;  //点下时候获取的x坐标
    private float upX;   //手指离开时候的x坐标


//    public Cursor1Adapter(Context context, Cursor c) {
//        super(context, c);
//        this.cursor=c;
//        mContext = context;
//        mInflater = LayoutInflater.from(context);
//        // TODO Auto-generated constructor stub
//    }


    public   Cursor1Adapter(List<Data> list,Context context) {


        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.list = list;

    }


    public int getCount() {
        return this.list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }



    @SuppressLint("ViewHolder")
    public View getView(final int i, View view, ViewGroup arg2)
    {
            this.i=i;
            Data use=list.get(i);
            view = LayoutInflater.from(mContext).inflate(R.layout.layout_tongxun_phone, arg2,false);
            imageView=view.findViewById(R.id.img_view_layout_phome);


            TextView phone = view.findViewById(R.id.id_layouttx_tv_phone);
            TextView   name = view.findViewById(R.id.id_layouttx_tv_name);
            TextView catalog = view.findViewById(R.id.catalog);


        String catalog1 = list.get(i).getFirstLetter();
        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if(i== getPositionForSection(catalog1)){
            catalog.setVisibility(View.VISIBLE);
            catalog.setText(use.getFirstLetter().toUpperCase());
        }else{
            catalog.setVisibility(View.GONE);
        }

            imageView.setImageResource(list.get(i).getIcon());
            phone.setText(this.list.get(i).getPhome());
             name.setText(this.list.get(i).getName());


        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext,"img"+i,Toast.LENGTH_LONG).show();
                Intent intent=new Intent(mContext,Tongxu_img_Activity.class);
                intent.putExtra("ID",list.get(i).getId());
                intent.putExtra("name",list.get(i).getName());
                intent.putExtra("phome",list.get(i).getPhome());
                intent.putExtra("address",list.get(i).getAddress());
                intent.putExtra("major",list.get(i).getMajar());
                intent.putExtra("qq",list.get(i).getQq());
                intent.putExtra("sex",list.get(i).getSex());
                mContext.startActivity(intent);
            }
        });


        return view;

    }




    /**
     * 获取catalog首次出现位置
     */
    public int getPositionForSection(String catalog) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = list.get(i).getFirstLetter();
            if (catalog.equalsIgnoreCase(sortStr)) {
                return i;
            }
        }
        return -1;
    }







}
